﻿using Esosi.Entity;
using Esosi.Repository.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace Esosi.Repository
{
    public class RegisterCalcRespository : IRegisterCalcRespository
    {
        private readonly HisContext _context;

        public RegisterCalcRespository(HisContext context)
        {
            _context = context ?? throw new ArgumentNullException(nameof(context));
        }

        public IEnumerable<GmChargePolicy> GetHosptialProtol(BalanceType balanceType, long subTypeId)
        {
            //            select* from GM_CHARGE_POLICY gcp, Gr_Charge_Policy_Reference gpr
            //WHERE gcp.BALANCE_TYPE_CODE = 900901
            //AND gcp.REFERENCE_ID = gpr.REFERENCE_ID
            //AND gpr.SUB_TYPE_ID = 1000000081

            try
            {
                return _context.GmChargePolicys.
                  Where(p => p.GrChargePolicyReference.SubTypeId == subTypeId
                          && p.BalanceType == balanceType
                          && p.GrChargePolicyReference.Status == "1")
                  .Include(p => p.GmChargePolicyDetails)
                  .ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public async Task<IEnumerable<GmPrice>> GetRegisterPricesAsync(RegisterCalcModel registerCalcModel)
        {
            var cdrf = _context.CrDeptRegFees.FirstOrDefault(p => p.DeptId == registerCalcModel.DeptId && p.ClinicType == registerCalcModel.ClinicType && p.RegisterType == registerCalcModel.RegisterType);
            return await _context.GmPrices.Where(p => p.PriceId == cdrf.DiagPriceId || p.PriceId == cdrf.RegPriceId).ToListAsync();
        }

        public Guid SaveRegisterJsonInfo(string StrRegisterJson)
        {
            try
            {
                var GuidId = new Guid();
                CtJsonRegisterInfo ctJsonRegisterInfo = new CtJsonRegisterInfo()
                {
                    JsonGuidId = GuidId,
                    StrJsonInfo = StrRegisterJson
                };
                _context.CtJsonRegisterInfos.Add(ctJsonRegisterInfo);
                _context.SaveChanges();

                return GuidId;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}